package com.impc.controller;

import com.impc.exception.ErrorEnum;
import com.impc.exception.MyException;
import com.impc.model.table.SysUser;
import com.impc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value = "/")
    public String login() {
        return "login";
    }

    /**
     * 登录处理
     * @param username
     * @param password
     * @param model
     * @return
     */
    @RequestMapping(value = "/check")
    public String check(@RequestParam("username") String username,@RequestParam("password") String password, Model model) {
        model.addAttribute("username", username);
        model.addAttribute("password", password);
        SysUser sysUser=userService.login(username,password);
        if(sysUser==null)
            throw new MyException(ErrorEnum.LOGIN_ERROR);
        model.addAttribute("sysUser",sysUser);
        return "home/index";
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout() {
        return "login";
    }
}
